package com.yiwu.web.initialize;

import com.yiwu.netty.NettyServer;
import jakarta.annotation.Resource;
import lombok.RequiredArgsConstructor;
import org.springframework.boot.CommandLineRunner;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.http.HttpMethod;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

/**
 * @author 咏鹅
 * @version 1.0
 * @description 初始化调用接口
 * @date 2024/1/30 17:56
 */
@Component
@RequiredArgsConstructor
public final class InitializeServletHandler implements CommandLineRunner {

    private final ConfigurableEnvironment configurableEnvironment;

    @Resource
    private NettyServer nettyServer;

    @Override
    public void run(String... args) throws Exception {
        new Thread(nettyServer).start();
        String url = String.format("http://127.0.0.1:%s%s",
                configurableEnvironment.getProperty("server.port", "5080") + configurableEnvironment.getProperty("server.servlet.context-path", ""),
                "/init");
        RestTemplate restTemplate = new RestTemplate();
        try {
            restTemplate.execute(url, HttpMethod.GET, null, null);
        } catch (Throwable ignored) {
        }
    }
}